﻿using System;
using DataAccess.Models;
using DataAccess.Repository;

namespace KPI
{
	public partial class ProgressReport : BasePage
	{
		private IKPIProgressRepository _KPIProgressRepository;
		private IKPIRepository _KPIRepository;
		private DataAccess.Models.KPI _TheKPI;

		public int TheKPIID
		{
			get
			{
				return CommonHelper.QueryStringInt("kpiid");
			}
		}

		public DataAccess.Models.KPI TheKPI
		{
			get
			{
				if(_TheKPI == null && TheKPIID > 0)
				{
					if(_KPIRepository == null)
					{
						_KPIRepository = new DataAccess.Repository.KPIRepository();
					}

					_TheKPI = _KPIRepository.GetTheKPI(TheKPIID);
				}

				return _TheKPI;
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				var canProgress = false;

				//var u = KPISiteContext.Current.CurrentUser;

				if(TheCurrentLoginUser != null && TheCurrentLoginUser.RoleName.Equals("unitadministrators"))
				{
					canProgress = (TheKPI.CurrentState == DataAccess.Models.KPIStates.Review_Approved ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Not_Approved ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Modified ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Approved ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Not_Approved ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Reporting ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Approved ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Not_Approved ||
													TheKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Request_Review_Not_Approved) && 
												(TheKPI.StartTime <= DateTime.Now && TheKPI.FinishTime >= DateTime.Now);
				}

				if(!canProgress)
				{
					Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
				}
			}
		}

		protected void cusvProgressContent_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				args.IsValid = (args.Value.Length <= 300);
			}
			catch
			{
				args.IsValid = false;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				lSeason.Text = CommonHelper.GetSeason();

				BindData();

				CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
				_FileBrowser.BasePath = "/ckfinder/";
				_FileBrowser.DisableThumbnailSelection = true;
				_FileBrowser.SetupCKEditor(CKEditorProgressContent);
			}
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{
				if(TheKPI != null)
				{
					var theKPIProgress = new KPIProgress()
					{
						KPIID = TheKPIID,
						KPIProgressAddTime = DateTime.Now,
						KPIProgressState = DataAccess.Models.KPIProgressStates.Processing
					};

					var season = CommonHelper.GetSeason();
					if(season == "一季度")
					{
						theKPIProgress.Quarter1Progress = CKEditorProgressContent.Text;
					}
					if(season == "二季度")
					{
						theKPIProgress.Quarter2Progress = CKEditorProgressContent.Text;
					}
					if(season == "三季度")
					{
						theKPIProgress.Quarter3Progress = CKEditorProgressContent.Text;
					}
					if(season == "四季度")
					{
						theKPIProgress.Quarter4Progress = CKEditorProgressContent.Text;
					}

					if(_KPIProgressRepository == null)
					{
						_KPIProgressRepository = new KPIProgressRepository();
					}

					if(_KPIProgressRepository.SaveKPIProgress(theKPIProgress, false))
					{
						TheKPI.PreState = TheKPI.CurrentState;
						TheKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Reporting;

						if(season == "一季度")
						{
							var season1EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, 3));

							if(season1EndDate >= TheKPI.FinishTime)
							{
								TheKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Report_End;
							}
						}
						else if(season == "二季度")
						{
							var season2EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, 6));

							if(season2EndDate >= TheKPI.FinishTime)
							{
								TheKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Report_End;
							}
						}
						else if(season == "三季度")
						{
							var season3EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, 9));

							if(season3EndDate >= TheKPI.FinishTime)
							{
								TheKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Report_End;
							}
						}
						else if(season == "四季度")
						{
							TheKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Report_End;
						}

						//if(season == "一季度" || season == "四季度")
						//{
						if(_KPIRepository.UpdateKPI(TheKPI, false))
						{
							Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
						}
						else
						{
							ShowMessage("进度上报时遇到错误,请稍后再试或联系相关技术人员.", true);
						}
						//}
						//else
						//{
						//  Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
						//}
					}
					else
					{
						ShowMessage("进度上报时遇到错误,请稍后再试或联系相关技术人员.", true);
					}
				}
			}
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_KPIProgressRepository != null)
			{
				((IDisposable)_KPIProgressRepository).Dispose();
			}

			if(_KPIRepository != null)
			{
				((IDisposable)_KPIRepository).Dispose();
			}

			if(CKEditorProgressContent != null)
			{
				CKEditorProgressContent.Dispose();
			}
		}

		private void BindData()
		{
			if(TheKPI != null)
			{
				this.lKPIContent.Text = TheKPI.KPIContent;

				if(TheKPI.TheKPIProgress != null)
				{
					var season = CommonHelper.GetSeason();
					var tagReadOnly = false;

					if(season == "一季度")
					{
						if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter1Progress))
						{
							CKEditorProgressContent.Text = TheKPI.TheKPIProgress.Quarter1Progress;
							tagReadOnly = true;
						}
					}
					if(season == "二季度")
					{
						if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter2Progress))
						{
							CKEditorProgressContent.Text = TheKPI.TheKPIProgress.Quarter2Progress;
							tagReadOnly = true;
						}
					}
					if(season == "三季度")
					{
						if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter3Progress))
						{
							CKEditorProgressContent.Text = TheKPI.TheKPIProgress.Quarter3Progress;
							tagReadOnly = true;
						}
					}
					if(season == "四季度")
					{
						if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter4Progress))
						{
							CKEditorProgressContent.Text = TheKPI.TheKPIProgress.Quarter4Progress;
							tagReadOnly = true;
						}
					}

					if(tagReadOnly)
					{
						CKEditorProgressContent.ReadOnly = true;
						btnSubmit.Visible = false;
					}
				}
			}
			else
			{
				btnSubmit.Visible = false;
			}
		}
	}
}
